package com.magiccompass.alipay.barcode.service;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.magiccompass.alipay.barcode.entity.AlipayBarcodePaymentRequest;
import com.magiccompass.alipay.barcode.entity.AlipayQRCodeNotification;
import com.magiccompass.alipay.barcode.entity.AlipayQRCodeTransaction;
import com.magiccompass.alipay.barcode.entity.MerchantInfo;
import com.magiccompass.alipay.barcode.entity.MerchantQRCode;

@Service("alipayBarcodeService")
@Transactional
public class AlipayBarcodeServiceImpl implements AlipayBarcodeService {
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	 
    public AlipayBarcodeServiceImpl() {
    }
    
	@Override
	public void save(Object entity){
		sessionFactory.getCurrentSession().saveOrUpdate(entity);
	}
	
	@Override
	public AlipayBarcodePaymentRequest findAlipayBarcodePaymentRequestByMerchantTransactionId(Integer id){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM AlipayBarcodePaymentRequest abpr WHERE abpr.merchantTransaction.id = :inputId order by id desc";
		Query query = session.createQuery(hql);
		query.setParameter("inputId",id);
		if(query.list() != null && query.list().size()>0){
			return (AlipayBarcodePaymentRequest)query.list().get(0);
		}else{
			return null;
		}
	}
	
	@Override
	public MerchantInfo findMerchantByAlipayQRCode(String qrCode){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM MerchantQRCode mqrc WHERE mqrc.qrCode = :qrCode";
		Query query = session.createQuery(hql);
		query.setParameter("qrCode",qrCode);
		if(query.list() != null && query.list().size()>0){
			MerchantQRCode mqrCode = (MerchantQRCode)query.list().get(0);
			return mqrCode.getMerchant();
		}else{
			return null;
		}
	}
	
	@Override
	public AlipayQRCodeNotification findAlipayQRCodeNotificationByNotifyId(String notifyId){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM AlipayQRCodeNotification aqrcn WHERE aqrcn.notifyId = :notifyId";
		Query query = session.createQuery(hql);
		query.setParameter("notifyId",notifyId);
		if(query.list() != null && query.list().size()>0){
			return (AlipayQRCodeNotification)query.list().get(0);
		}else{
			return null;
		}
	}
	
	@Override
	public MerchantQRCode findMerchantQRCodeByMerchantId(Integer merchantId){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM MerchantQRCode mqrc WHERE mqrc.merchant.id = :merchantId";
		Query query = session.createQuery(hql);
		query.setParameter("merchantId",merchantId);
		if(query.list() != null && query.list().size()>0){
			MerchantQRCode mqrCode = (MerchantQRCode)query.list().get(0);
			return mqrCode;
		}else{
			return null;
		}
	}
	
	@Override
	public AlipayQRCodeNotification findAlipayQRCodeNotificationByMTId(Integer mtId){
		Session session = sessionFactory.getCurrentSession();
		String hql = "FROM AlipayQRCodeTransaction aqrct WHERE aqrct.mt.id = :mtId";
		Query query = session.createQuery(hql);
		query.setParameter("mtId",mtId);
		if(query.list() != null && query.list().size()>0){
			AlipayQRCodeTransaction aqrct = (AlipayQRCodeTransaction)query.list().get(0);
			return aqrct.getQrCodeNotification();
		}else{
			return null;
		}
	}
}